package com.config.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.config.entity.EquMold;
import com.config.entity.Equipment;
import com.config.service.EquMoldService;
import com.config.service.EquipmentService;


@Controller
@RequestMapping("/equMold")
public class EquMoldController {

	@Resource(name="equMoldService")
	private EquMoldService equMoldService;
	@Resource(name="equipmentService")
	private EquipmentService equipmentService;
	
	
	/**
	 * 状态主界面
	 * @return
	 */
	@RequestMapping("/moldMain")
	public String stateMain(Model model){
		model.addAttribute("currentName","设备基本参数配置");
		return "config/equipMoldConfig";
	}
	
	/**
	 * 分页查询所有记录
	 * @param model
	 * @param start
	 * @param limit
	 * @return
	 */
	@RequestMapping("/findByAll")
	public @ResponseBody Model findByLine(Model model,
			@RequestParam(value = "page", required = false, defaultValue="0") int start,
			@RequestParam(value = "rows", required = false, defaultValue="10") int limit){
		model.addAttribute("total",equMoldService.countByAll());
		model.addAttribute("rows",equMoldService.findByAll(start, limit));
		return model;
	}
	
	/**
	 * 查询所有设备类型
	 */
	@RequestMapping("/findAll")
	public List<Map<String,Object>> findAll(){
		List<EquMold> list=equMoldService.findAll();
		List<Map<String,Object>> value=new ArrayList<Map<String,Object>>();
		if(list.size()>0){
			for(EquMold e:list){
				Map<String,Object> map=new HashMap<String,Object>();
				map.put("id", e.getId());
				map.put("text", e.getEquType());
				value.add(map);
			}
		}
		return value;
	}
	
	@RequestMapping("/save")
	public Model save(Model model,EquMold entity){
		equMoldService.save(entity);
		model.addAttribute("success",true);
		model.addAttribute("msg","保存成功");
		return model;
	}
	
	@RequestMapping("/load")
	public Model load(Model model,int id){
		EquMold entity=equMoldService.getEquMold(id);
		model.addAttribute("data",entity);
		model.addAttribute("success",true);
		model.addAttribute("msg","加载成功");
		return model;
	}
	
	@RequestMapping("/update")
	public Model update(Model model,EquMold entity){
		equMoldService.update(entity);
		model.addAttribute("success",true);
		model.addAttribute("msg","修改成功");
		return model;
	}
	
	@RequestMapping("/delete")
	public Model delete(Model model,int id){
		List<Equipment> equipList=equipmentService.findByEquModlId(id);
		if(equipList.size()>0){
			model.addAttribute("success",false);
			model.addAttribute("msg","该类型已被引用，不能删除");
		}else{
			equMoldService.delete(id);
			model.addAttribute("success",true);
			model.addAttribute("msg","删除成功");
		}
		return model;
	}
}
